package nu.mine.tbje.easyEDT;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;

import com.megginson.sax.DataWriter;

import nu.mine.tbje.easyEDT.data.Cours;

public class XMLWriter {

	public static String getWeek(int week, int id, OutputStream out) {
		try {
			Edt edt = new Edt();
			edt.setLogin("adeweb");
			edt.setPass("ade423");
			edt.doLogin();
			edt.doInit();
			edt.setIdAndCat("trainee", id);
			edt.setDisplayOptions();
			edt.setWeek(week);
			InputStream i = edt.getData();
			Iterator<Cours> iterator = edt.parse(i).iterator();
			Cours cours = null;
			DataWriter w = new DataWriter(new OutputStreamWriter(out));
			w.setIndentStep(2);
			w.startDocument();
			w.startElement("week");
			while (iterator.hasNext()){
				cours = iterator.next();
				w.startElement("class");
				w.dataElement("name", cours.getName());
				w.dataElement("date", cours.getDate());
				w.dataElement("time", cours.getTime());
				w.dataElement("duration", cours.getDuration());
				w.dataElement("room", cours.getRoom());
				w.dataElement("teacher", cours.getTeacher());
				w.endElement("class");
			}
			w.endElement("week");
			w.endDocument();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "ok";
	}
}
